const api = require("../../../../utils/apiUtils");
const app = getApp();

Page({
  data: {
    tradeNo:null,
    pwdStr:null,
    timer:null,
    times:15
  },
  _loadPayResult:function(){
    let tradeNo = this.data.tradeNo;
    const _self = this;
    api.post(api.url.pclResult,{
      tradeNo:tradeNo,
      payType:2
    },res=>{
        if(res.data.code == 200){
          if(res.data.body.resultCode == 2){
              if(_self.data.timer){
                  clearInterval(_self.data.timer);
                  _self.data.timer = null;
              }
              
              _self.setData({
                  pwdStr:res.data.body.pwdStr
              })
          }
        }else if(res.data.msg){
          my.showModal({
              content: res.data.msg,
              showCancel:false,
              complete: (res) => {
                  my.reLaunch({
                    url: '/pages/index/index',
                  })
              }
            })
        }else{
            my.showToast({
              content: '获取信息失败',
              type:'error'
            })
            setTimeout(function(){
              my.reLaunch({
                  url: '/pages/index/index',
                })
            });
        }
    })
  },

  btnChangePwd:function(){
    const _self = this;
    my.showModal({
        title:'温馨提示',
        content: '是否要切换密码？请确定已输入了当前密码',
        complete: (res) => {
            if (res.confirm) {
                api.post(api.url.pclChangePwd,{
                    tradeNo:_self.data.tradeNo
                },res=>{
                    console.info(res);
                      if(res.data.code==200){
                        my.showToast({content: '切换成功',icon:'none'})
                        _self.setData({pwdStr:res.data.body.pwdStr})
                      }else if(res.data.msg){
                        my.showModal({
                            content: res.data.msg,
                            showCancel:false
                          })
                      }else {
                        my.showToast({content:'切换失败',icon:'error'})
                      }
                })
            }
        }
    })
  },
  onLoad(options) {
    this.data.tradeNo = options.tradeNo;
      const _self = this;
      this.data.timer = setInterval(()=>{
          _self._loadPayResult();
          _self.data.times = _self.data.times  - 1;
          if(_self.data.times < 0){
            clearInterval(_self.data.timer);
            my.showModal({
                content: '支付无结果，请重新扫码或联系客服',
                showCancel:false,
                complete: (res) => {
                    my.reLaunch({
                      url: '/pages/index/index',
                    })
                }
            })
          }
      },1000)
  },
});
